import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { ValidationPipe, VersioningType } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { EnvEnum } from './common/enums/env.enum';
import helmet from 'helmet';
import { NestExpressApplication } from '@nestjs/platform-express';
import { setupLogger } from './common/logger/log';
import { setGlobalApp } from './common/global';
import * as path from 'path';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    logger: setupLogger()
  });
  app.useStaticAssets(path.join(__dirname, '../files'), {
    prefix: '/static/',
  });

  const config = new DocumentBuilder()
    .setTitle('snowball API')
    .setDescription('The API description')
    .setVersion('1.0')
    .addTag('snowball')
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('doc', app, document);

  app.use(helmet());

  // 使用版本控制
  app.enableVersioning({
    type: VersioningType.URI, // URI 版本控制
    prefix: 'api/v', // 设置前缀为 api/v
    defaultVersion: '1', // 默认版本为 1
  });

  //全局验证管道
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
      transform: true,
    }),
  );

  app.enableCors();
  const configService = app.get(ConfigService);
  await app.listen(configService.get(EnvEnum.APP_PORT) ?? 3000, '0.0.0.0');

  setGlobalApp(app);
}
bootstrap();
